var orgDialog;
var orgDialogAdd;

$(function() {
	//初始化组织datagrid
	$("#org_table").datagrid({
		url: currenthost + '/org/query.ajax',
		title: '组织编码',
		iconCls: 'icon-edit',
		pagination: true,
		pageSize: 10,
		pageList: [10, 20, 30],
		columns: [[{
			field: 'id',
			checkbox: true
		},
		{
			field: 'orgCode',
			title: '组织编码',
			width:  150
		},
		{
			field: 'group',
			title: '组织名称',
			width:  250
		}]],
		toolbar: [{
			text: '增加',
			iconCls: 'icon-add',
			handler: function() {
				admin_org_add()
			}
		},
		'-', {
			text: '删除',
			iconCls: 'icon-remove',
			handler: function() {
				admin_org_removeFun();
			}
		},
		'-', {
			text: '编辑',
			iconCls: 'icon-edit',
			handler: function() {
				admin_org_editFun();
			}
		}]
	});

	//初始化组织userDialog
	orgDialog = $('#orgDialog').show().dialog({
			modal: true,
			title: '组织信息',
			width: 300,
			height: 200,
			buttons: [{
				text: '确定',
				handler: function() {
					$('#orgForm').form('submit', {
						url: currenthost + "/org/update.ajax",
						success: function(data) {
							var r = jQuery.parseJSON(data);
							if (r.success) {
								orgDialog.dialog('close');
								jQuery.messager.show({
									msg: r.msg,
									title: '提示'
								});
								$('#org_table').datagrid('reload');
							}else{
								jQuery.messager.show({
									msg: r.msg,
									title: '提示'
								});
							}
						}
					});
				}
			}],
			onClose: function () {
		             //解决弹出窗口关闭后，验证消息还显示在最上面
		             $('.validatebox-tip').remove();
		     }
		}).dialog('close');

	orgDialogAdd = $('#orgDialogAdd').show().dialog({
		modal: true,
		title: '增加组织',
		width: 300,
		height: 200,
		buttons: [{
			text: '增加',
			iconCls: 'icon-add',
			handler: function() {
				$('#admin_org_addForm').form('submit', {
					url: currenthost + '/org/add.ajax',
					success: function(result) {
						var r = jQuery.parseJSON(result);
						if (r.success) {
							orgDialogAdd.dialog('close');
							$('#org_table').datagrid('load');
						}
						jQuery.messager.show({
							title: '提示',
							msg: r.msg
						});
					}
				});
			}
		}],
	   onClose: function () {
             //解决弹出窗口关闭后，验证消息还显示在最上面
             $('.validatebox-tip').remove();
         }
	}).dialog('close');


})


function admin_org_editFun() {
		var orgForm = $('#orgForm').form();
		var rows = $("#org_table").datagrid('getSelections');
		if (rows.length != 1 && rows.length != 0) {
			jQuery.messager.show({
				msg: '只能择一个组织进行编辑！您已经选择了' + rows.length + '个组织',
				title: '提示'
			});
		} else if (rows.length == 1) {
			orgForm.find('[name=id]').attr('readonly', 'readonly');
			orgDialog.dialog('open');
			orgForm.form('clear');
			orgForm.form('load', {
				id: rows[0].id,
				group: rows[0].group,
				orgCode: rows[0].orgCode
			});
		}else if (rows.length == 0) {
			jQuery.messager.show({
				msg: '请选择一个组织！',
				title: '提示'
			});
		}
	}


function admin_org_add() {
	$("#org_table").datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	var admin_org_addForm = $('#admin_org_addForm').form();
	admin_org_addForm.form('clear');
	orgDialogAdd.dialog('open');
}

function admin_org_removeFun() {
	var rows = $('#org_table').datagrid('getChecked');
	var ids = [];
	if (rows.length > 0) {
		jQuery.messager.confirm('确认', '您是否要删除当前选中的项目？',
		function(r) {
			if (r) {
				for (var i = 0; i < rows.length; i++) {
					ids.push(rows[i].id);
				}
				jQuery.ajax({
					url: currenthost + '/org/delete.ajax',
					data: {
						ids: ids.join(',')
					},
					dataType: 'json',
					success: function(result) {
						if (result.success) {
							$('#org_table').datagrid('load');
							$('#org_table').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
						}
						jQuery.messager.show({
							title: '提示',
							msg: result.msg
						});
					}
				});
			}
		});
	} else {
		jQuery.messager.show({
			title: '提示',
			msg: '请勾选要删除的记录！'
		});
	}
}

//查询
function admin_org_searchFun() {
	$('#org_table').datagrid('load', serializeObject($('#admin_org_searchForm')));
}

//清空条件
function admin_org_cleanFun() {
	$('#admin_org_searchForm').form('clear');
}

var serializeObject = function(form) {
	var o = {};
	jQuery.each(form.serializeArray(),
	function(index) {
		if (o[this['name']]) {
			o[this['name']] = o[this['name']] + "," + this['value'];
		} else {
			o[this['name']] = this['value'];
		}
	});
	return o;
};